package frontend.pages.admin.sfi;

import backend.model.admin.Activity;
import backend.model.admin.Department;
import backend.model.admin.Occupation;
import backend.service.admin.ActivityService;
import backend.service.admin.DepartmentService;
import backend.service.admin.OccupationService;
import backend.service.admin.SfiService;
import frontend.components.other.DBModelChoiceRenderer;
import frontend.core.TimeTrackingWebApplication;
import frontend.model.admin.SfiUIModel;
import frontend.pages.admin.base.AbstractManagingForm;
import frontend.components.base.ComponentFactory;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;

import java.util.ArrayList;
import java.util.List;

public class SfiForm extends AbstractManagingForm<SfiUIModel> {

    private static final String RESOURCE_KEY_FIELD_LABEL_NAME = "pages.admin.sfi.form.name";
    private static final String RESOURCE_KEY_FIELD_LABEL_DESCRIPTION = "pages.admin.sfi.form.description";
    private static final String RESOURCE_KEY_FIELD_LABEL_ACTIVITY = "pages.admin.sfi.form.activity";
    private static final String RESOURCE_KEY_FIELD_LABEL_DEPARTMENT = "pages.admin.sfi.form.description";
    private static final String RESOURCE_KEY_FIELD_LABEL_IS_ACTIVE = "pages.admin.sfi.form.isActive";

    private static final String RESOURCE_KEY_VALIDATION_SAME_SFI_NAME = "validation.form.sfi.sameSfiName";

    public SfiForm(String id, SfiUIModel sfiUIModel) {
        super(id, sfiUIModel);
    }

    @Override
    protected void constructFormContent() {
        Panel nameComponent = ComponentFactory.createTextFieldComponent("name", RESOURCE_KEY_FIELD_LABEL_NAME, uiModel);
        Panel descriptionComponent = ComponentFactory.createTextFieldComponent("description", RESOURCE_KEY_FIELD_LABEL_DESCRIPTION, uiModel);
        WebMarkupContainer activityComponent = ComponentFactory.createDropDownChoiceComponent("activity", RESOURCE_KEY_FIELD_LABEL_ACTIVITY, uiModel, getAvailableActivities(), new DBModelChoiceRenderer<Activity>(), false);
        WebMarkupContainer departmentComponent = ComponentFactory.createDropDownChoiceComponent("department", RESOURCE_KEY_FIELD_LABEL_DEPARTMENT, uiModel, getAvailableDepartments(), new DBModelChoiceRenderer<Department>(), false);
        Panel isActiveComponent = ComponentFactory.createCheckboxComponent("isActive", RESOURCE_KEY_FIELD_LABEL_IS_ACTIVE, uiModel);

        add(nameComponent, descriptionComponent, activityComponent, departmentComponent, isActiveComponent);
    }

    @Override
    protected Class getResponsePage() {
        return SfiManagingPage.class;
    }

    @Override
    protected void doFormValidation() {
        validateNotEmpty(uiModel.getName(), RESOURCE_KEY_FIELD_LABEL_NAME);
        validateNotEmpty(uiModel.getDescription(), RESOURCE_KEY_FIELD_LABEL_DESCRIPTION);
        validateNotEmpty(uiModel.getActivity(), RESOURCE_KEY_FIELD_LABEL_ACTIVITY);
        validateNotEmpty(uiModel.getDepartment(), RESOURCE_KEY_FIELD_LABEL_DEPARTMENT);
        if ((uiModel.getName() != null) && nameWasChanged()) {
            SfiService service =  TimeTrackingWebApplication.getService(SfiService.class);
            if (service.findSfiByName(uiModel.getName()) != null) {
                addFieldValidationError(RESOURCE_KEY_FIELD_LABEL_NAME, RESOURCE_KEY_VALIDATION_SAME_SFI_NAME);
            }
        }
    }

    private boolean nameWasChanged() {
        return (false == uiModel.getName().equals(uiModel.getDBModel().getName()));
    }

    private List<Activity> getAvailableActivities() {
        List<Activity> availableActivities = new ArrayList<Activity>();
        availableActivities.addAll(TimeTrackingWebApplication.getService(ActivityService.class).findAllActiveActivities());
        return availableActivities;
    }

    private List<Department> getAvailableDepartments() {
        List<Department> availableDepartments = new ArrayList<Department>();
        availableDepartments.addAll(TimeTrackingWebApplication.getService(DepartmentService.class).findAllActiveDepartments());
        return availableDepartments;
    }
}
